<template>
  <div>
    <vxe-toolbar id="toolbarId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
        <el-button v-if="!parentData.showInfo" @click="inviteTheDriver">邀请加入</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      highlight-hover-row
      row-id="id"
      height="700"
      :data="tableData"
      :loading="loading"
      remote-sort
      @sort-change="sortList"
    >
      <vxe-table-column v-if="!parentData.showInfo" type="checkbox" width="60" />
      <vxe-table-column>
        <vxe-table-column field="name" title="司机姓名" show-header-overflow show-overflow min-width="130" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.name" placeholder="请输入司机姓名" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="phone" title="手机号" show-header-overflow show-overflow min-width="130" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.phone" placeholder="请输入手机号" clearable />
        </template>
      </vxe-table-column>

    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />

  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import XEUtils from 'xe-utils'
import VXEUtils from 'vxe-utils'
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ['cookie'] })
import { getInviteDriverList, inviteDriver } from '@/api/driver'
export default {
  name: 'InviteTheDriver',
  props: {
    parentData: {
      type: Object,
      default() {
        return {
          showInfo: false
        }
      }
    }
  },
  data() {
    return {
      loading: false,
      tableData: [],
      checkedRowArray: [],
      listQuery: {
        teamCode: '',
        name: '',
        phone: '',
        pageNum: 1,
        pageSize: 10,
        sortField: '',
        sortType: '',
        total: 0
      }
    }
  },
  created() {
    this.listQuery.teamCode = this.$route.query.partyCode ? this.$route.query.partyCode : ''
    this.findList()
  },
  methods: {
    findList() {
      this.loading = true
      return new Promise(resolve => {
      getInviteDriverList(this.listQuery).then(response => {
        this.tableData = response.data.records
        this.listQuery.total = response.data.total
        this.loading = false
        resolve();
      })
      })
    },
    // 邀请司机
    inviteTheDriver() {
      // 获取所有选中的复选框
      this.checkedRowArray = this.$refs.xTable.getSelectRecords()
      if (!this.checkedRowArray || this.checkedRowArray.length === 0) {
        this.$message({ message: '请选择要邀请的司机', type: 'warning' })
      } else {
        const checkCodeArray = []
        this.checkedRowArray.forEach(item => {
          checkCodeArray.push(item.partyCode)
        })
        const checkPartyCodes = checkCodeArray.join(',')
        inviteDriver({ teamCode: this.listQuery.teamCode, driverCodes: checkPartyCodes }).then(response => {
          this.$message({ message: '邀请成功', type: 'success' })
          this.findList()
        })
      }
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property
        this.listQuery.sortType = order
      } else {
        this.listQuery.sortField = ''
      }
      this.findList()
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage
      this.listQuery.pageSize = pageSize
      this.findList()
    }

  }
}
</script>
